<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\AlarmPlan;
use Illuminate\Http\Request;

class AlarmPlanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data=$request->query();
        $where[]=['is_deleted',0];
        if (isset($data['plan_title']))
        {
            $where[] = ['plan_title', 'like' ,"%{$data['plan_title']}%"];
        }
        if (isset($data['status']))
        {
            $where['alarm_type'] = $data['status'];
        }
        if (isset($data['change']))
        {
            $query = AlarmPlan::query()
                ->whereDate('create_time', '>=',
                    date("Y-m-d H:i:s",strtotime($data['change'][0])))
                ->whereDate('create_time', '<=',
                    date("Y-m-d H:i:s",strtotime($data['change'][1])))
                ->where($where)
                ->orderBy('is_default')
                ->orderBy('id','desc')
                ->paginate($request->per_page ?? 30);
        }else {
            $query = AlarmPlan::query()
                ->where($where)
                ->orderBy('is_default')
                ->orderBy('id','desc')
                ->paginate($request->per_page ?? 30);

        }
        return $this->success($query);
    }
    public function EditAlarmPlan($id = null,AlarmPlan $AlarmPlan)
    {

        if ($post=$_POST){

            if (isset($post['id']))
            {
                $query = $AlarmPlan
                    ->where('id' , $post['id'])
                    ->update($post);
            }else{
                $query = $AlarmPlan->insert($post);
            }

            return $this->success($query);
        }else {
            $query = AlarmPlan::query()
                ->where('id', $id)
                ->orderBy('id', 'desc')
                ->first();

            return $this->success($query);
        }

    }
    public function editAlarmPlanStatus(AlarmPlan $AlarmPlan)
    {

        $data=$_POST;
        if ($data){;
            $info = $AlarmPlan
                ->where('id', $data['id'])
                ->first();
            if ($info[$data['type']] == 0)
            {
                $query = $AlarmPlan
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>1]);

            }else if ($info[$data['type']] == 1){

                $query = $AlarmPlan
                    ->where('id' , $data['id'])
                    ->update([$data['type']=>0]);
            }
            return $this->success($query);
        }
        return $this->error();

    }
    public function editAlarmPlanDel($id)
    {
        $ids=explode(',',$id);
        $query = AlarmPlan::query()
            ->whereIn('id' , $ids)
            ->update(['is_deleted'=>1]);
        return $this->success($query);

    }

    /**
     * Display the specified resource.
     *
     * @param AlarmPlan $meter_model
     * @param int $id
     * @return array|Response
     */
    public function show(AlarmPlan $meter_model, $id)
    {
        $info = $meter_model->find($id);
        return $this->success($info);
    }

}
